﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace FigureDraw.Graphic.GDIPlus
{
    public class GDIPlusSVGGraphic : CommonGraphic
    {
        string filename;
        StringBuilder svgContent;
        public GDIPlusSVGGraphic(string filename, MyPoint size)
        {
            this.filename = filename;

            svgContent = new StringBuilder();
            svgContent.AppendLine(string.Format("<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{0}pt\" height=\"{1}pt\" viewBox=\"0 0 {0} {1}\" version=\"1.1\"></svg>", size.X, size.Y));
        }

        public override void DrawLine(MyPoint startPoint, MyPoint endPoint, MyColorInfo strokeColor, double strokeWidth)
        {
            svgContent.Insert(svgContent.Length - 8,
                string.Format("<line x1=\"{0}\" y1=\"{1}\" x2=\"{2}\" y2=\"{3}\" style=\"stroke:rgb({4},{5},{6});stroke-width:{7}\" />",
                startPoint.X, startPoint.Y, endPoint.X, endPoint.Y, strokeColor.R, strokeColor.G, strokeColor.B, strokeWidth));
        }

        public override void DrawRectangle(MyPoint position, MyPoint size, MyColorInfo strokeColor, double strokeWidth, MyColorInfo brushColor)
        {
            svgContent.Insert(svgContent.Length - 8,
                string.Format("<rect x=\"{0}\" y=\"{1}\" width=\"{2}\" height=\"{3}\" style=\"stroke:rgb({4},{5},{6});stroke-width:{7}", position.X, position.Y, size.X, size.Y, strokeColor.R, strokeColor.G, strokeColor.B, strokeWidth));

            if (brushColor == null)
                svgContent.Insert(svgContent.Length - 8, ";fill-opacity:0\"/>");
            else
                svgContent.Insert(svgContent.Length - 8, string.Format(";fill-opacity:1;fill:rgb({0},{1},{2})\"/>", brushColor.R, brushColor.G, brushColor.B));
        }

        public override void DrawRoundedRectangle(MyPoint position, MyPoint size, MyColorInfo strokeColor, double strokeWidth, MyColorInfo brushColor)
        {
            svgContent.Insert(svgContent.Length - 8,
                string.Format("<rect x=\"{0}\" y=\"{1}\" rx=\"{2}\" ry=\"{3}\" width=\"{4}\" height=\"{5}\" style=\"stroke:rgb({6},{7},{8});stroke-width:{9}",
                position.X, position.Y, 20, 20, size.X, size.Y, strokeColor.R, strokeColor.G, strokeColor.B, strokeWidth));

            if (brushColor == null)
                svgContent.Insert(svgContent.Length - 8, ";fill-opacity:0\"/>");
            else
                svgContent.Insert(svgContent.Length - 8, string.Format(";fill-opacity:1;fill:rgb({0},{1},{2})\"/>", brushColor.R, brushColor.G, brushColor.B));
        }

        public override void DrawEllipse(MyPoint position, MyPoint size, MyColorInfo strokeColor, double strokeWidth, MyColorInfo brushColor)
        {
            svgContent.Insert(svgContent.Length - 8,
                string.Format("<ellipse cx=\"{0}\" cy=\"{1}\" rx=\"{2}\" ry=\"{3}\" style=\"stroke:rgb({4},{5},{6});stroke-width:{7}",
                position.X + size.X / 2, position.Y + size.Y / 2, size.X / 2, size.Y / 2, strokeColor.R, strokeColor.G, strokeColor.B, strokeWidth));

            if (brushColor == null)
                svgContent.Insert(svgContent.Length - 8, ";fill-opacity:0\"/>");
            else
                svgContent.Insert(svgContent.Length - 8, string.Format(";fill-opacity:1;fill:rgb({0},{1},{2})\"/>", brushColor.R, brushColor.G, brushColor.B));
        }

        public override void DrawText(MyPoint position, string text, double size, MyColorInfo strokeColor)
        {
            svgContent.Insert(svgContent.Length - 8,
                string.Format("<text x=\"{0}\" y=\"{1}\" font-size=\"{2}\" style=\"fill:rgb({3},{4},{5})\">{6}</text>", position.X, position.Y, size, strokeColor.R, strokeColor.G, strokeColor.B, text));
        }

        public override void DrawPolygon(MyColorInfo strokeColor, double strokeWidth, MyColorInfo brushColor, params MyPoint[] points)
        {
            svgContent.Insert(svgContent.Length - 8, string.Format("<polygon points=\""));
            
            foreach(var point in points)
                svgContent.Insert(svgContent.Length - 8, string.Format("{0},{1} ", point.X, point.Y));

            svgContent.Insert(svgContent.Length - 8, string.Format("\" style=\"stroke:rgb({0},{1},{2});stroke-width:{3}",
                strokeColor.R, strokeColor.G, strokeColor.B, strokeWidth));

            if (brushColor == null)
                svgContent.Insert(svgContent.Length - 8, ";fill-opacity:0\"/>");
            else
                svgContent.Insert(svgContent.Length - 8, string.Format(";fill-opacity:1;fill:rgb({0},{1},{2})\"/>", brushColor.R, brushColor.G, brushColor.B));
        }

        public override void DrawCurve(MyColorInfo strokeColor, double strokeWidth, MyPoint[] points)
        {
            svgContent.Insert(svgContent.Length - 8, string.Format("<path d=\"M {0} {1} q ", points[0].X, points[0].Y));

            for (int i = 1; i < points.Length; i++)
                svgContent.Insert(svgContent.Length - 8, string.Format("{0} {1} ", points[i].X - points[0].X, points[i].Y - points[0].Y));

            svgContent.Insert(svgContent.Length - 8, string.Format("\" style=\"stroke:rgb({0},{1},{2});stroke-width:{3};fill-opacity:0\"/>",
                strokeColor.R, strokeColor.G, strokeColor.B, strokeWidth));
       
        }

        override public void DrawPicture(List<MyShape> shapes)
        {
            base.DrawPicture(shapes);
            File.WriteAllText(filename, svgContent.ToString());
            
        }

    }
}
